from rest_framework.views import exception_handler
from rest_framework.response import Response
from rest_framework import status

# from common.utils import result_error

result_error = {}


def custom_exception_handler(exc, context):
    response = exception_handler(exc, context)
    if not response:  # response 为空
        result_error['result'] = {
            'code': status.HTTP_500_INTERNAL_SERVER_ERROR,
            'errors': '服务器内部错误'
        }
        response = Response(result_error, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        return response

    result_error['result'] = {
        'code': response.status_code,
        'errors': response.data.get('detail') or response.data
    }
    response.data = result_error
    return response
